package asteroids.model.statements;

import java.util.Map;

import asteroids.model.*;
import asteroids.model.expressions.E;
import asteroids.model.expressions.Variable;
import asteroids.model.types.T;

/**
 * An action statement that turns his executer a given angle
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public class Turn extends ExpressionStatement implements Action {

	private Ship executer;
	private E validatedAngle;
	
	public Turn(E angle) {
		super(angle);
	}
	
	public void setValidatedAngle(E angle) {
		this.validatedAngle = angle;
	}
	
	public E getValidatedAngle() {
		return validatedAngle;
	}
	
	@Override
	public void action() throws IllegalArgumentException{
		assert executer != null;
		if (!hasProperAngle()) {throw new IllegalArgumentException(); }
		executer.turn((Double)validatedAngle.getValue());
	}
	
	private boolean hasProperAngle() {
		if(getValidatedAngle() == null)
			return false;
		return getValidatedAngle().getReturnType().equals("double");
	}

	@Override
	public void setExecuter(Ship ship) {
		this.executer = ship;
		getExpression().setExecuter(ship);
	}

	@Override
	public boolean typeCheck(Map<String, T> globals) {
		if(getExpression() instanceof Variable) {
			if(globals.get(getExpression().getValue()) == null)
				return false;
			else 
				return globals.get(getExpression().getValue()).getReturnType().equals("double");	
		}
		return getExpression().typeCheck(globals);
	}
	
	

}
